//
//  AppDelegate.swift
//  WeiboProjectYL
//
//  Created by yanli on 16/7/9.
//  Copyright © 2016年 mountainPassCell. All rights reserved.
//

import UIKit

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    //  返回观察者对象
    var observer: NSObjectProtocol?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        // 观察者 接受通知
//        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVC:", name: SwitchRootVCNotification, object: nil)
        
        observer = NSNotificationCenter.defaultCenter().addObserverForName(SwitchRootVCNotification, object: nil, queue: nil, usingBlock: { [weak self] (noti) -> Void in
            
            //切换根视图控制器
            self?.switchRootVC(noti)
        })
        
        
        //  如果是自己定义的类, 想要转成对象需要 -在前面拼接工程名.类名
        //  如果这个类是我系统自己,转成对象不需要使用工程名.类名
        //  同类名获取字符串
        //        let result =  NSStringFromClass(CZProfileTableViewController.self)
        //        print(result)

        //        //  类名字符串
        //        let className = "WHWeiBo01.CZProfileTableViewController"
        //        //  转换相应类型
        //        let myClass = NSClassFromString(className) as? UIViewController.Type
        //        //  创建对象使用init
        //        let vc = myClass!.init()
        //        print(vc)
        
        
        
        // 1.创建window
        self.window = UIWindow(frame: (UIScreen.mainScreen().bounds))
        // 2.创建根控制器
        let rootVC = SKMainViewController()
        // 3.设置根控制器
        self.window?.rootViewController = rootVC
        // 4.设置为主窗口并显示
        self.window?.makeKeyAndVisible()

        return true
    }

   
    //接受调用通知 方法
    @objc private func switchRootVC(noti: NSNotification) {
        // 接受 通知 object
        let object = noti.object
        // 好比isKindOfClass
        // 如果通知是第三方的登录控制器发送的
        if object is SKOAuthViewController {
            //设置根控制器
            window?.rootViewController = SKWelcomeViewController()
        } else {
            window?.rootViewController = SKMainViewController()
        }
    }
    
    
    
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

    
    deinit {
        print("移除通知")
        //销毁 移除通知
        NSNotificationCenter.defaultCenter().removeObserver(observer!)
        
    }

}

